package com.amt.basic.service.impl;

import com.amt.basic.config.ResultMsg;
import com.amt.basic.service.OrderService;
import com.amt.util.HttpUtil;
import com.amt.util.JsonUtil;
import com.amt.util.WeiMengApiUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class OrderServiceImpl  implements OrderService {
    public String createRights(String json) {
        ResultMsg resultMsg = new ResultMsg();
        log.info("售后信息变更订阅消息{}",json);
        Map map = JsonUtil.jsonToHashMap(json);
        HashMap<String,Object> body = (HashMap<String, Object>) map.get("msg_body");
        //售后单号
        Long rightsId = (Long) body.get("rightsId");
        //订单编号
        Long orderNo = (Long) body.get("orderNo");
        //门店编号
        Long storeId = (Long) body.get("storeId");
        //拿到消息  变更的商品
        if(rightsId!=null ){
            String result = getOrderByNo(orderNo);
            log.info("订单详情查询返回{}",result);
            Map resultMap = JsonUtil.jsonToHashMap(result);
            //拿到信息再查询订单
            resultMsg.setErrcode("0");
            resultMsg.setErrmsg("success");
            return JsonUtil.objectToJson(resultMsg);
        }
        resultMsg.setErrcode("0");
        resultMsg.setErrmsg("error");
        return JsonUtil.objectToJson(resultMsg);
    }

    @Override
    public String createOrder(String json) {
        ResultMsg resultMsg = new ResultMsg();
        log.info("新增订单订阅消息{}",json);
        Map map = JsonUtil.jsonToHashMap(json);
        HashMap<String,Object> body = (HashMap<String, Object>) map.get("msg_body");
        //售后单号
        Long orderNo = (Long) body.get("orderNo");
        //订单创建时间
        String createTime =body.get("createTime").toString();
        //订单类型
        Long orderType = (Long) body.get("orderType");
        //拿到订单号 查询订单
        if(orderNo!=null ){
            String result = getOrderByNo(orderNo);
            log.info("订单详情查询返回{}",result);
            Map resultMap = JsonUtil.jsonToHashMap(result);
            //拿到信息再查询订单
            resultMsg.setErrcode("0");
            resultMsg.setErrmsg("success");
            return JsonUtil.objectToJson(resultMsg);
        }
        resultMsg.setErrcode("0");
        resultMsg.setErrmsg("error");
        return JsonUtil.objectToJson(resultMsg);
    }


    /**
     * 根据订单id查询订单详情
     * @param orderNo
     * @return
     */
    public String getOrderByNo(Long orderNo){
        HashMap<String, Long> hashMap = new HashMap<>();
        hashMap.put("orderNo",orderNo);
        String result = HttpUtil.requestPost(WeiMengApiUtil.ORDER_INFO_QUERY, JsonUtil.objectToJson(hashMap));
        return result;
    }
}
